package com.yulan.bankback.service.bank.impl;

import com.yulan.bankback.service.bank.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    private ValueOperations<String, String> ops;

    SetOperations<String, String> setOps;

    HashOperations<String,String,String> hashOps;

    ListOperations<String, String> listOps;


    @PostConstruct
    public void init() {
        ops = redisTemplate.opsForValue();
        setOps = redisTemplate.opsForSet();
        hashOps = redisTemplate.opsForHash();
        listOps = redisTemplate.opsForList();
    }

    @Override
    public String get(String key) {
        return ops.get(key);
    }

    @Override
    public List<String> multiGet(List<String> keys) {
        return ops.multiGet(keys);
    }

    @Override
    public void expire(String key, long ttl, TimeUnit timeUnit) {
        redisTemplate.expire(key, ttl, timeUnit);
    }

    @Override
    public void set(String key, String value, long ttl, TimeUnit timeUnit) {
        ops.set(key, value, ttl, timeUnit);
    }

    @Override
    public void set(String key, String value) {
        ops.set(key, value);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public boolean sIsMember(String key, String val) {
        return setOps.isMember(key, val);
    }

    @Override
    public void sadd(String set, String val) {
        setOps.add(set, val);
    }

    @Override
    public void srem(String set, String val) {
        setOps.remove(set, val);
    }

    public Set<String> sMembers(String set){
        return setOps.members(set);
    }

    @Override
    public boolean setNx(String key, String value) {
        return ops.setIfAbsent(key, value);
    }

    @Override
    public long incrBy(String key, long num) {
        return ops.increment(key, num);
    }

    @Override
    public long getLong(String key) {
        String value = get(key);
        if (StringUtils.isBlank(value)) {
            return 0;
        }
        return Long.parseLong(value);
    }

    @Override
    public void multiDel(Set<String> keys) {
        redisTemplate.delete(keys);
    }

    @Override
    public void lpush(String list, String val) {
        listOps.leftPush(list, val);
    }

    @Override
    public String rpop(String list) {
        return listOps.rightPop(list);
    }

    @Override
    public Long listSize(String key) {
        return listOps.size(key);
    }

    @Override
    public void hSet(String key, String field, String value, long ttl, TimeUnit timeUnit) {
        redisTemplate.expire(key, ttl, timeUnit);
        hashOps.put(key,field,value);
    }

    @Override
    public Map<String, String> hGetAll(String key) {
        return hashOps.entries(key);
    }

    @Override
    public String hGetByKey(String mapName, String key) {
        return hashOps.get(mapName, key);
    }

    @Override
    public Long hDelByKey(String mapName, String key) {
        return hashOps.delete(mapName, key);
    }

    @Override
    public void hDel(String key, List<String> hashKeys) {
        hashOps.delete(key,hashKeys.toArray());
    }

    @Override
    public boolean hasKey(String key, String hashKey) {
        return hashOps.hasKey(key,hashKey);
    }

    @Override
    public Long ttl(String key) {
        Long ttl = redisTemplate.getExpire(key);
        return ttl == null ? 0 : ttl * 1000;
    }
}
